import wx
from PublicMethod import *
'''
ListLabelCtrl : 

    A list box with no background color, no scroll bar and can be controlled only by direction key
    
    on keyboard.
'''
class ListLabelCtrl():
    '''
    Constructor : ListLabelCtrl
        panel - this object is added to panel
        startPos - the position of the list control
    '''
    def __init__(self, panel, startPos):
        self._001_initialInstanceVariable(panel, startPos)
        self._003_createSelectionFrame()
    '''
    function _001 : initialize the variables used for this object
        panel - this object is added to panel
        startPos - the position of the list control
    '''
    def _001_initialInstanceVariable(self, panel, startPos):
        #this object is added to panel
        self.panel = panel
        #the position of the list control
        self.startPos = startPos
        #the list label of all selection
        self.listLabel = []
        #the directory name of selection
        self.dir = None
        #start index of file into current selection showing
        self.curStartFileIndex = 0
        #the index of currently chosen
        self.curSelectIndex = 0
    '''
    function _002 : set selection to referred index
        index - index of selection
    '''
    def _002_setSelection(self, index):
        self.listLabel[self.curSelectIndex].SetBackgroundColour("black")
        self.listLabel[self.curSelectIndex].Show(False)
        self.listLabel[self.curSelectIndex].Show(True)
        self.curSelectIndex = index
        self.listLabel[self.curSelectIndex].SetBackgroundColour("red")
        self.listLabel[self.curSelectIndex].Show(False)
        self.listLabel[self.curSelectIndex].Show(True)
    '''
    function _003 : create label of selection of amount - MAX_SEL_APPEAR
    '''
    def _003_createSelectionFrame(self):
        index = 0
        while index < MAX_SEL_APPEAR :
            label =  wx.StaticText(self.panel, -1, "", pos = (self.startPos[0], self.startPos[1] + index * 40))
            label.SetFont(wx.Font(15, wx.SWISS, wx.NORMAL, wx.BOLD))
            label.SetSize((180,30))
            label.SetForegroundColour("yellow")
            self.listLabel.append(label)
            index = index + 1
    '''
    function ACT001 : initialize all selection from startIndex in passing directory - dir and set 
                      selection to index - selIndex
    '''
    def ACT001_initialListLabel(self, dir, startIndex, selIndex):
        self.dir = dir
        self.curStartFileIndex = startIndex
        self.curSelectIndex = -1
        #get selection text
        listName = PublicMethod.ACT005_getDirInSpecDir(dir, self.curStartFileIndex, MAX_SEL_APPEAR)
        #clear current selection
        index = 0
        while index < MAX_SEL_APPEAR :
            self.listLabel[index].SetBackgroundColour("black")
            self.listLabel[index].Show(False)
            index = index + 1
        #set new selection
        index = 0
        for name in listName :
            self.listLabel[index].SetLabel(name)
            self.listLabel[index].SetSize((180,30))
            self.listLabel[index].SetBackgroundColour("black")
            self.listLabel[index].Show(True)
            index = index + 1
        #if number of selection is at least one, set current selection
        if index > 0 :
            self.curSelectIndex = 0
            self._002_setSelection(selIndex)
    '''
    function ACT002 : clear all label of this selector
    '''
    def ACT002_clearListLabel(self):
        index = 0
        while index < self.listLabel.__len__() :
            self.listLabel[index].Destroy()
            index = index + 1
        self.listLabel = []
    '''
    function ACT003 : set to next selection
    '''
    def ACT003_setNextSelection(self):
        if self.curSelectIndex >= MAX_SEL_APPEAR - 1 : #current selection is at bottom
            array = PublicMethod.ACT005_getDirInSpecDir(self.dir, self.curStartFileIndex + MAX_SEL_APPEAR, MAX_SEL_APPEAR)
            if array.__len__() > 0 :  #it exists another next selection list
                self._002_setSelection(0)
                self.ACT001_initialListLabel(self.dir, self.curStartFileIndex + MAX_SEL_APPEAR, 0)
        elif self.listLabel[self.curSelectIndex + 1].IsShown(): #the next selection exists
            self._002_setSelection(self.curSelectIndex + 1)
    '''
    function ACT004 : set to before selection
    '''
    def ACT004_setBeforeSelection(self):
        if self.curStartFileIndex > 0 and self.curSelectIndex == 0 : #current selection is at top
            array = PublicMethod.ACT005_getDirInSpecDir(self.dir, self.curStartFileIndex - MAX_SEL_APPEAR, MAX_SEL_APPEAR)
            if array.__len__() > 0 : #it exists before selection list
                self._002_setSelection(MAX_SEL_APPEAR - 1)
                self.ACT001_initialListLabel(self.dir, self.curStartFileIndex - MAX_SEL_APPEAR, MAX_SEL_APPEAR - 1)
        elif self.curSelectIndex > 0: 
            self._002_setSelection(self.curSelectIndex - 1)
    '''
    function ACT005 : destroy all object of this class
    '''
    def ACT005_destroy(self):
        index = 0
        while index < self.listLabel.__len__() :
            self.listLabel[index].Destroy()
            index = index + 1
    '''
    function GET001 : get text of current chosen label
    '''
    def GET001_curSelLabelValue(self):
        if self.curSelectIndex != -1 :
            return self.listLabel[self.curSelectIndex].GetLabelText()
        else :
            return ""
    '''
    function GET002 : get current chosen index
    '''
    def GET002_curSelMapIndex(self):
        return self.curStartFileIndex + self.curSelectIndex